import React from 'react';
import moment from 'moment';
import { Form, Row, Col, DatePicker, Radio, Switch, Icon, InputNumber, Tooltip } from 'antd';
import {
    renderOptionValue,
    renderSwitchValue,
    momentToString,
    renderHiddenField,
    renderFormText,
    MAX_NUMBER
} from '@/utils/utils';
import { useCompanyData } from '@/hooks';

import { RETAIN_TYPE_LIST, SEVER_TYPE_ENUM, TYPE_LIST_ENUM } from '../constants';

const currentMoment = moment();

const Append = ({ form, formValueMap, isEdit, applyTime, employeeData = {} }) => {
    const { getFieldDecorator, getFieldValue } = form;

    // 又来了又来了
    const severType = getFieldValue('severType');
    const isFixDate = SEVER_TYPE_ENUM.NO_RENEW === severType;
    const date = getFieldValue('applyDimissionDate');
    const autoDate = moment(applyTime || currentMoment).add(30, 'd'); // 申请日+30天，用户TM能知道？

    // 职责链?
    const getDateText = () => {
        if (!isEdit) return null;
        if (isFixDate) {
            // 不删掉就是让各位看看原先的业务设计有多糟糕
            return '离职日期为合同期满之日';
            // return null;
        }
        if (!date) return null;
        if (momentToString(date, 'YYYY-MM-DD') === momentToString(getFieldValue('finalDimissionDate'), 'YYYY-MM-DD')) {
            return null;
        }
        return (
            <span style={{ fontSize: 12 }}>
                员工期望离职日期为<span style={{ color: 'red' }}>{momentToString(date, 'YYYY-MM-DD')}</span>
                ，请与员工确认
            </span>
        );
    };

    const dateValue = isFixDate ? employeeData.regularData : formValueMap.finalDimissionDate;
    const { data: companyData = {} } = useCompanyData(employeeData.administrativeOrganizationCode);
    const unit = companyData.currencyCode || '元';

    const isActiveType = TYPE_LIST_ENUM.ACTIVE === getFieldValue('dimissionType');
    const showCompensation = Boolean(getFieldValue('compensationFlag'));

    const showFlagText = Boolean(getFieldValue('dimissionFillVacancyFlag'));
    return (
        <>
            <Row>
                <Col span={24}>
                    <Form.Item label="确认离职日期" extra={getDateText()}>
                        {isEdit ? (
                            getFieldDecorator('finalDimissionDate', {
                                rules: [{ required: true, message: '离职日期不能为空' }],
                                initialValue: autoDate
                            })(
                                <DatePicker
                                    style={{ width: '80%' }}
                                    getCalendarContainer={trigger => trigger.parentNode}
                                />
                            )
                        ) : (
                            <>
                                {momentToString(dateValue, 'YYYY-MM-DD', true)}
                                {renderHiddenField({
                                    field: 'finalDimissionDate',
                                    initialValue: dateValue,
                                    form
                                })}
                            </>
                        )}
                    </Form.Item>
                </Col>
            </Row>

            {isActiveType && (
                <Row>
                    <Col span={24}>
                        <Form.Item label="留任方式">
                            {isEdit ? (
                                getFieldDecorator('retainType', {
                                    initialValue: 0
                                })(
                                    <Radio.Group>
                                        {RETAIN_TYPE_LIST.map(item => (
                                            <Radio key={item.value} value={item.value}>
                                                {item.text}
                                            </Radio>
                                        ))}
                                    </Radio.Group>
                                )
                            ) : (
                                <>
                                    {renderOptionValue(RETAIN_TYPE_LIST, formValueMap.retainType)}
                                    {renderHiddenField({
                                        field: 'retainType',
                                        initialValue: formValueMap.retainType,
                                        form
                                    })}
                                </>
                            )}
                        </Form.Item>
                    </Col>
                </Row>
            )}

            {!isActiveType && showCompensation && (
                <Row>
                    <Col span={24}>
                        <Form.Item
                            label={
                                <span>
                                    经济补偿金&nbsp;
                                    <Tooltip title="按劳动者在本单位工作的年限，每满一年支付一个月工资。六个月以上不满一年的，按一年计算，不满六个月的，支付半个月工资，最高不超过十二年。如果月工资高于本地区上年度职工月平均工资三倍，需按职工月平均工资的三倍替代月工资来计算。">
                                        <Icon style={{ color: 'rgba(0,0,0,0.25)' }} type="question-circle-o" />
                                    </Tooltip>
                                </span>
                            }
                        >
                            {isEdit ? (
                                getFieldDecorator('compensation')(
                                    <InputNumber
                                        min={0}
                                        max={MAX_NUMBER}
                                        placeholder="请输入"
                                        style={{ width: '80%' }}
                                    />
                                )
                            ) : (
                                <>
                                    {renderFormText(formValueMap.compensation)}
                                    {renderHiddenField({
                                        field: 'compensation',
                                        initialValue: formValueMap.compensation,
                                        form
                                    })}
                                </>
                            )}
                            <span> {unit}</span>
                        </Form.Item>
                    </Col>
                </Row>
            )}

            <Row>
                <Col span={24}>
                    <Form.Item label="岗位补缺">
                        {isEdit ? (
                            getFieldDecorator('dimissionFillVacancyFlag', {
                                valuePropName: 'checked',
                                normalize: value => Number(value || null)
                            })(<Switch />)
                        ) : (
                            <>
                                {renderSwitchValue(formValueMap.dimissionFillVacancyFlag)}
                                {renderHiddenField({
                                    field: 'dimissionFillVacancyFlag',
                                    initialValue: Number(formValueMap.dimissionFillVacancyFlag || null),
                                    form
                                })}
                            </>
                        )}
                        {isEdit && showFlagText && (
                            <span style={{ color: 'rgba(0, 0, 0, 0.45)', paddingLeft: 6, fontSize: 12 }}>
                                选择后，系统将在审批完成后提醒部门主管发起招聘需求申请，若审批被拒绝则不提醒
                            </span>
                        )}
                    </Form.Item>
                </Col>
            </Row>
        </>
    );
};

export default Append;
